package com.huang.controller;

import cn.hutool.json.JSONObject;
import com.huang.service.IDemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @describe: 由于是面试题，所以4个接口全写在这个Controller类了，就不分多个了
 *            我这边也把实体类改成json对象了这样方面些编码，操作数据库的部分，我直接写sql语句，就使用mybatis或mybatis plus
 * @param:
 * @return:
 * @author: huang
 * @date: 2023/10/18 18:17
 */
@Api(tags = "Java面试题接口")
@RestController
@RequestMapping("/demo")
@RequiredArgsConstructor
public class DemoController {

    @Autowired
    private IDemoService demoService;

    /**
     * @describe: 查询用户余额接口
     * @param:
     * @return:
     * @author: huang
     * @date: 2023/10/18 18:17
     */
    @ApiOperation("查询用户余额接口")
    @GetMapping("queryBalance")
    public JSONObject queryBalance(@RequestBody @Validated JSONObject userDTO){
        JSONObject resultJson = new JSONObject();
        try{
            resultJson=demoService.queryBalance(userDTO);
        }catch (Exception e) {
            log.error("查询用户余额接口(/demo/queryBalance)出现了异常:",e);
        }
        return resultJson;//前端就取resultJson该json对象的balance即可,(resultJson.get("balance"))
    }


    /**
     * @describe: 用户余额消费100元接口
     * @param:
     * @return:
     * @author: huang
     * @date: 2023/10/18 18:54
     */
    @ApiOperation("用户余额消费100元接口")
    @PostMapping("consume")
    public JSONObject consume(@RequestBody @Validated JSONObject consumeDTO){
        JSONObject resultJson = new JSONObject();
        try{
            resultJson=demoService.consume(consumeDTO);
        }catch (Exception e) {
            log.error("用户余额消费元接口(/demo/consume)出现了异常:",e);
        }
        return resultJson;//前端知道消费结果就行，成功或者失败
    }

    /**
     * @describe: 用户退款20元接口
     * @param:
     * @return:
     * @author: huang
     * @date: 2023/10/18 18:54
     */
    @ApiOperation("用户退款20元接口")
    @PostMapping("refund")
    public JSONObject refund(@RequestBody @Validated JSONObject refundDTO){
        JSONObject resultJson = new JSONObject();
        try{
            resultJson=demoService.refund(refundDTO);
        }catch (Exception e) {
            log.error("用户退款接口(/demo/refund)出现了异常:",e);
        }
        return resultJson;//前端知道退款结果就行，成功或者失败
    }

    /**
     * @describe: 查询用户余额变动明细接口
     * @param:
     * @return:
     * @author: huang
     * @date: 2023/10/18 18:54
     */
    @ApiOperation("查询用户余额变动明细接口")
    @PostMapping("query/balance/change")
    public JSONObject queryBalanceChange(@RequestBody @Validated JSONObject changeDTO){
        JSONObject resultJson = new JSONObject();
        try{
            resultJson=demoService.queryBalanceChange(changeDTO);
        }catch (Exception e) {
            log.error("查询用户余额变动明细接口(/demo/query/balance/change)出现了异常:",e);
        }
        return resultJson;//前端最好是以报表形式展示给用户
    }

}

